/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.

 * Author: James Harpe
 * 
 * 
 * Description: This activity handles user options regarding account and event notifications.
 * 
 */

package us.in.lib.acpl.Mobile_ACPL;

import java.util.ArrayList;

import com.google.analytics.tracking.android.EasyTracker;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseActivity;
import us.in.lib.acpl.Mobile_ACPL.util.JSONArrayPrefs;

public class SettingsActivity extends BaseActivity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.getActionBar().setTitle("  Settings");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		SharedPreferences prefs = this.getSharedPreferences("myPrefs",
				Context.MODE_WORLD_WRITEABLE);

		
		boolean notifyDueDates = prefs.getBoolean("notifyDueDates",
				true);
		CheckBox c = (CheckBox) findViewById(R.id.notifyDueDates);
		c.setChecked(notifyDueDates);

		boolean notifyHolds = prefs.getBoolean("notifyHolds", true);
		c = (CheckBox) findViewById(R.id.notifyHolds);
		c.setChecked(notifyHolds);

		String daysAhead = prefs.getString("daysAhead", "3");
		Spinner spinner = (Spinner) findViewById(R.id.notifyDueDatesTimeSelector);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.days, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinner.setAdapter(adapter);
		spinner.setSelection(Integer.parseInt(daysAhead) - 1);

		String hoursAhead = prefs.getString("hoursAhead", "3");
		int hours = Integer.parseInt(hoursAhead);
		if (hours == 48) {
			hoursAhead = "9";
		} else if (hours == 24) {
			hoursAhead = "8";
		}
		spinner = (Spinner) findViewById(R.id.notifyEventsTimeSelector);
		adapter = ArrayAdapter.createFromResource(this, R.array.hours,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinner.setAdapter(adapter);
		spinner.setSelection(Integer.parseInt(hoursAhead) - 1);

		this.findViewById(R.id.clearRememberedEvents).setOnClickListener(
				new ClearListener());
		this.findViewById(R.id.settingsSave).setOnClickListener(this);
		this.findViewById(R.id.settingsCancel).setOnClickListener(this);

	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		TextView button = (TextView) view;
		String t = button.getText().toString();
		if (t.equalsIgnoreCase("Save")) {

			SharedPreferences prefs = this.getSharedPreferences("myPrefs",
					Context.MODE_WORLD_WRITEABLE);
			SharedPreferences.Editor editor = prefs.edit();

		

			CheckBox c = (CheckBox) findViewById(R.id.notifyDueDates);
			boolean checked = c.isChecked();
			editor.putBoolean("notifyDueDates", checked);
			if (checked) {
				Spinner spinner = (Spinner) findViewById(R.id.notifyDueDatesTimeSelector);
				int pos = spinner.getSelectedItemPosition();
				editor.putString("daysAhead", String.valueOf(pos + 1));
			}

			c = (CheckBox) findViewById(R.id.notifyHolds);
			checked = c.isChecked();
			editor.putBoolean("notifyHolds", checked);

			Spinner spinner = (Spinner) findViewById(R.id.notifyEventsTimeSelector);
			int pos = spinner.getSelectedItemPosition();
			if (pos == 7) {
				pos = 24;
			} else if (pos == 8) {
				pos = 48;
			} else {
				pos = pos + 1;
			}
			editor.putString("hoursAhead", String.valueOf(pos));

			editor.commit();
			this.finish();
		} else {
			this.finish();
		}
	}

	private class ClearListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {


			
			ArrayList<String> events = JSONArrayPrefs.getStringArrayPref(
					arg0.getContext(), "events");
			events.clear();
			JSONArrayPrefs.setStringArrayPref(arg0.getContext(), "events", events);

			Toast toast = Toast.makeText(arg0.getContext(),
					"All events cleared", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}

	}
	


}
